//
//  UTSimulateClickViewController.swift
//  UniversalTool
//
//  Created by LGB on 2025/3/8.
//

import UIKit

class UTSimulateClickViewController: UTBaseViewController {
    
    var handler:((UTSimulateModel) -> Void)?
    var model: UTSimulateModel?
    
    @IBOutlet weak var frequencySlider: UISlider!
    @IBOutlet weak var frequencyLabel: UILabel!
    @IBOutlet weak var frequencyCenterX: NSLayoutConstraint!
    @IBOutlet weak var durationSlider: UISlider!
    @IBOutlet weak var durationLabel: UILabel!
    @IBOutlet weak var durationCenterX: NSLayoutConstraint!
    @IBOutlet weak var closeButton: UIButton!
    @IBOutlet weak var openButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(white: 0, alpha: 0.3)
        frequencySlider.addTarget(self, action: #selector(sliderValueChanged(_:)), for: .valueChanged)
        durationSlider.addTarget(self, action: #selector(sliderValueChanged(_:)), for: .valueChanged)
        if let model = model {
            frequencySlider.value = Float(model.frequency)
            frequencyLabel.text = "\(model.frequency)次"
            frequencyCenterX.constant = frequencySlider.frame.origin.x + CGFloat(frequencySlider.value / frequencySlider.maximumValue) * frequencySlider.frame.width - frequencySlider.frame.origin.x - frequencySlider.frame.width / 2 + 5
            
            durationSlider.value = Float(model.duration)
            durationLabel.text = "\(model.duration)次"
            durationCenterX.constant = durationSlider.frame.origin.x + CGFloat(durationSlider.value / durationSlider.maximumValue) * durationSlider.frame.width - durationSlider.frame.origin.x - durationSlider.frame.width / 2 + 5
            
            closeButton.isSelected = false
            openButton.isSelected = false
            model.soundStatus ? (openButton.isSelected = true) : (closeButton.isSelected = true)
        }
    }
    
    
    @IBAction func didClose(_ sender: Any) {
        dismiss(animated: false)
    }
    
    @objc func sliderValueChanged(_ sender: UISlider) {
        updateValueLabelPosition(sender)
    }
    
    func updateValueLabelPosition(_ sender: UISlider) {
        guard let model = model else { return }
        let value = Int(sender.value)
        if sender == frequencySlider {
            model.frequency = value
            frequencyLabel.text = "\(value)次"
            let centerX = frequencySlider.frame.origin.x + CGFloat(frequencySlider.value / frequencySlider.maximumValue) * frequencySlider.frame.width
            frequencyCenterX.constant = centerX - frequencySlider.frame.origin.x - frequencySlider.frame.width / 2 + 5
        }else if sender == durationSlider {
            model.duration = value
            let centerX = durationSlider.frame.origin.x + CGFloat(durationSlider.value / durationSlider.maximumValue) * durationSlider.frame.width
            durationLabel.text = "\(value)秒"
            durationCenterX.constant = centerX - durationSlider.frame.origin.x - durationSlider.frame.width / 2 + 5
        }
    }
    
    @IBAction func soundChangeStatus(_ sender: UIButton) {
        guard let model = model else { return }
        if sender.isSelected { return }
        sender.isSelected = !sender.isSelected
        if sender == closeButton{
            openButton.isSelected = false
            model.soundStatus = false
        }else if sender == openButton{
            closeButton.isSelected = false
            model.soundStatus = true
        }
    }
    
    @IBAction func save(_ sender: UIButton) {
        if let handler = handler {
            guard let model = model else { return }
            handler(model)
        }
        dismiss(animated: false)
    }
    
}
